package com.example.bo.homeclass;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.example.bo.homeclass.beans.LoginBean;
import com.example.bo.homeclass.model.UserModel;

public class MainActivity extends AppCompatActivity implements RetrofitListener<LoginBean> {
    private EditText usernameview,passwordview;
    private Button loginButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        initEvent();
    }

    private void initViews() {
        usernameview = (EditText) findViewById(R.id.editText);
        passwordview = (EditText) findViewById(R.id.editText2);
        loginButton = (Button) findViewById(R.id.button);

    }

    private void initEvent() {
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (usernameview.getText().toString().equals("") || passwordview.getText().toString().equals("")) {
                    Toast.makeText(MainActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    login();
                }
            }
        });
        usernameview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()){

                    case(R.id.button):
                        Intent intent1 = new Intent(MainActivity.this,HomeActivity.class);
                        startActivity(intent1);
                        break;
                }
            }
        });
    }

    private void login() {
        UserModel userModel = new UserModel();
        userModel.userLogin(usernameview.getText().toString().trim(), passwordview.getText().toString().trim(), this);
    }


    public void onSuccess(LoginBean userId, int flag) {
        if (userId.getUserid().equals("0")) {
            Toast.makeText(MainActivity.this, "用户名或者密码错误", Toast.LENGTH_SHORT).show();
        } else {
            saveUser(userId.getUserid());
            Intent intent = new Intent(MainActivity.this, HomeActivity.class);
            startActivity(intent);
            Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
        }
    }


    public void onFail() {
        Toast.makeText(MainActivity.this, "网络出错了", Toast.LENGTH_SHORT).show();
    }

    public void saveUser(String user_id) {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("username", usernameview.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id", userid);
        editor.putString("userpass", passwordview.getText().toString());
        editor.commit();

    }
}
